﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Baopx_API;

namespace Baopx.BackEnd.ManageNews
{
    public partial class MangeNewsCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            grdNewsCategory.DataSource = A.Controller.NewsCategoryController.GetAllCategoryParent();
            grdNewsCategory.DataBind();
        }

        protected void ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    NewsCategory newsCategory = new NewsCategory();
                    newsCategory.Name = ((TextBox)e.Item.FindControl("txtName")).Text;
                    newsCategory.OrderDisplay = Convert.ToInt32(((TextBox)e.Item.FindControl("txtOrderDisplay")).Text);
                    newsCategory.IsActive = true;
                    A.Controller.NewsCategoryController.Insert(newsCategory);
                    break;
                case "cmdUpdate":
                    NewsCategory newsCategoryUpdate =
                        A.Controller.NewsCategoryController.GetElementById(Convert.ToInt32(e.CommandArgument));
                    newsCategoryUpdate.Name = ((TextBox)e.Item.FindControl("txtName")).Text;
                    newsCategoryUpdate.OrderDisplay = Convert.ToInt32(((TextBox)e.Item.FindControl("txtOrderDisplay")).Text);
                    newsCategoryUpdate.IsActive = ((CheckBox)e.Item.FindControl("chkActive")).Checked;
                    A.Controller.NewsCategoryController.Update();
                    break;
                case "cmdDelete":
                    NewsCategory newsCategoryDelete =
                        A.Controller.NewsCategoryController.GetElementById(Convert.ToInt32(e.CommandArgument));
                    A.Controller.NewsCategoryController.Delete(newsCategoryDelete);
                    break;
            }
            BindData();
        }

        protected void ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
            }
        }
    }
}